<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台首页控制器
//需要构造函数的时候在进行添加.
class ShowController extends Controller{
	/**
	 * 初始化
	 */
	private $model;
	private $uid = null;
	public function __init(){
		if(isset($_SESSION['uinfo']['uid'])){
			$this->uid = $_SESSION['uinfo']['uid'];
		}
		$this->model = new \Common\Model\Goods;
	}
	/**
	 * [index 商品展示页面]
	 * [需要用到的表有:]
	 * [goods表][里面的名字、市场价、商品价]
	 * [detail表][(里面的大图、小图、中图、商品详情、售后服务)]
	 * [goods_attr表][里面的规格(颜色、尺码)]
	 * [brand表][里面的bname]
	 * @return [type] [description]
	 */
	public function index(){
		// p($_SESSION);  //在这儿进行打印出购物车的信息
		$gid = Q('get.gid');
		// p(Q('get.gid'));
		// 或者可以采用关联表的关系去进行筛选
		$totalData = $this->model
				   ->join('detail','shop_goods.gid','=','shop_detail.gid') //关联的是商品详情表
				   ->join('brand','shop_goods.bid','=','shop_brand.bid')//关联的是品牌表
				   // ->join('goods_attr','shop_goods.gid','=','shop_goods_attr.gid')//商品属性表
				   ->where(array('shop_goods.gid'=>$gid))
				   ->find();
		// $totalData = Db::table('goods')
		// 	    ->join('detail','shop_goods.gid','=','shop_detail.gid')
		// 	    // ->join('contacts','user.id','=','contacts.uid')
		// 	    ->get();
		// p($totalData);die;
		// 将获取到的图片(这儿可能有多张图片，所以得将其进行组合成数组，然后在进行分配到页面中去)
		$totalData['small'] = explode(',', $totalData['small']);
		$totalData['medium'] = explode(',', $totalData['medium']);
		$totalData['big'] = explode(',', $totalData['big']);
		// p($totalData);
		$this->assign('totalData',$totalData);
		// 商品属性表得另外取
		$GattrModel = new \Common\Model\Gattr;
		$GattrData = $GattrModel
				   ->join('type_attr','shop_type_attr.taid','=','shop_goods_attr.type_taid')
				   ->where(array('gid'=>$gid))
				   ->where(array('shop_type_attr.class'=>1))
				   ->get();
		// p($GattrData);
		$attrData = array();
		foreach ($GattrData as $k => $v) {
			// p($value);
			$attrData[$v['taid']][0] = $v['taname'];
			$attrData[$v['taid']][1][$v['gtid']] = $v['gtvalue'];
		}
		// p($attrData);
		$this->assign('attrData',$attrData);
		// p($GattrData);
		$this->display();
	}


	public function getSizeAjax(){
		// $ajaxData = $_POST;
		// p($_POST);
		$data = $_POST['spec'];
		$data = rtrim($data,',');
		$newData = explode(',',$data);
		// p($newData);
		$gid = $_POST['gid'];
		$added = array();
		$GattrModel = new \Common\Model\Gattr;  //商品属性表
		foreach ($newData as $key => $value) {
			$added[] = $GattrModel->where(array('gid'=>$gid))->where(array('gtvalue'=>$value))->pluck('added');
		}
		// p($added);
		$price = $_POST['price'];
		foreach ($added as $key => $value) {
			$price += $value;
		}
		// p($price);
		$this->ajax($price);
	}
}